package DataStructures;

public class SingleLinkedList <T> {
	
	Node <T> head;
	
	public SingleLinkedList(){
		
		head = null;
	}
	
	public void insertAtHead(T e){
		
		Node<T> temp = new Node<T>(e);
		temp.next = head;
		head = temp;
	}
	
	public void insertAtEnd(T e){
		
		Node<T> temp = new Node<T>(e);
		Node<T> run = head;
		if (run != null){
			while (run.next != null){
				run = run.next;
			}
			run.next = temp;
		}
		else
			insertAtHead(e);
	}
	
	public void eraseHead(){
		
		if (head != null){
			head = head.next;
		}
	}
	
	public void eraseEnd(){
		
		Node<T> run = head;
		while (run.next.next != null){
			run = run.next;
		}
		run.next = null;
	}
	
	public void print(){
		
		Node<T> run = head;
		while (run.next != null){
			System.out.println(run.data);
			run = run.next;
		}
	}
	
	public Node<T> find(T e){
		
		while (head != null && head.data != e){
			
			head = head.next;
		}
		
		return null;
	}
	
	public void delete(T e){
		
		if (head.data == e){
			
			head = head.next;
		}
		else{
			Node<T> run = head;
			while (run.next != null){
				if (run.next.data == e ){
					run.next = run.next.next;
				}
				run = run.next;
			}
		}
	}
}
